package org.edu.modules.classSchedule.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.edu.common.api.vo.Result;
import org.edu.common.system.query.QueryGenerator;
import org.edu.common.system.vo.LoginUser;
import org.edu.common.util.oConvertUtils;
import org.edu.modules.classChoise.service.IJwKechengClassService;
import org.edu.modules.classSchedule.entity.VeJwSection;
import org.edu.modules.classSchedule.service.IVeJwSectionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.edu.modules.jxrw.entity.VeJwJxrw;
import org.edu.modules.scheduleCommon.entity.VeBaseAppUser;
import org.edu.modules.scheduleCommon.entity.VeBaseBanji;
import org.edu.modules.scheduleCommon.entity.VeBaseSemester;
import org.edu.modules.scheduleCommon.service.VeBaseAppUserService;
import org.edu.modules.scheduleCommon.service.VeBaseBanjiService;
import org.edu.modules.scheduleCommon.service.VeBaseSemesterService;
import org.edu.modules.scheduleCommon.service.VeBaseTeacherService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.edu.common.system.base.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.edu.common.aspect.annotation.AutoLog;

/**
 * @Description: 排课节信息
 * @Author:
 * @Date: 2021-05-12
 * @Version: V1.0
 */
@Api(tags = "排课节设置")
@RestController
@RequestMapping("/classSchedule/veJwSection")
@Slf4j
public class VeJwSectionController extends BaseController<VeJwSection, IVeJwSectionService> {

    @Autowired
    private IVeJwSectionService veJwSectionService;

    @Autowired
    private IJwKechengClassService iJwKechengClassService;

    @Autowired
    private VeBaseSemesterService veBaseSemesterService;

    @Autowired
    private VeBaseAppUserService veBaseAppUserService;

    @Autowired
    private VeBaseTeacherService veBaseTeacherService;

    @Autowired
    private VeBaseBanjiService veBaseBanjiService;

    /**
     * 分页查询
     *
     * @param veJwSection
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "排课节信息-分页查询")
    @ApiOperation(value = "列表查询", notes = "排课节信息-分页查询")
    @PostMapping(value = "/list")
    public Result<?> queryPageList(VeJwSection veJwSection,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "99") Integer pageSize,
                                   HttpServletRequest req) {
/*		QueryWrapper<VeJwSection> queryWrapper = QueryGenerator.initQueryWrapper(veJwSection, req.getParameterMap());
		Page<VeJwSection> page = new Page<VeJwSection>(pageNo, pageSize);
		IPage<VeJwSection> pageList = veJwSectionService.page(page, queryWrapper);*/

        QueryWrapper<VeJwSection> queryWrapper = QueryGenerator.initQueryWrapper(veJwSection, req.getParameterMap());
        Page<Map<String, Object>> page = new Page<Map<String, Object>>(pageNo, pageSize);
        IPage<Map<String, Object>> pageList = veJwSectionService.pageMaps(page, queryWrapper);
        List<Map<String, Object>> veJwSections = pageList.getRecords();
        for (Map<String, Object> map : veJwSections) {
            if (map.get("kcId") != null) {
                Integer kcid = Integer.parseInt(map.get("kcId").toString());
                String kcmc = iJwKechengClassService.selectCourseNameById(kcid);
                map.put("kcmc", kcmc);
            } else {
                map.put("kcmc", null);
            }
        }
        pageList.setRecords(veJwSections);
        return Result.OK(pageList);
    }

    /**
     * 通过学期id查班主任或者教师负责的班级，班主任查自己负责的班级，教师查负责的教学任务，排课表ve_jw_paike有教学班id
     *
     * @param semId
     * @return
     */
    @AutoLog(value = "调课-通过semId查询当前学期的班级")
    @ApiOperation(value = "调课-通过semId查询当前学期的班级", notes = "调课-通过semId查询当前学期的班级")
    @GetMapping(value = "/queryBanjiBySemId")
    public Result<?> queryBanjiBySemId(@RequestParam(name = "semId", required = false) Integer semId) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (oConvertUtils.isEmpty(loginUser)) {
            return Result.error("请登录系统！");
        }
        //根据用户id查用户信息
        VeBaseAppUser appUser = veBaseAppUserService.getVeBaseAppUserById(loginUser.getId());
        List<VeBaseBanji> banjiList = new ArrayList<>();
        if (appUser.getUserType().contains("1")) {
            //如果用户类型是1，为教师账号
            Integer currentSemId = 0;
            if (semId == null) {
                //查当前学期
                VeBaseSemester currentSemester = veBaseSemesterService.getCurrentSemester();
                currentSemId = currentSemester.getId();
            } else {
                currentSemId = semId;
            }
            List<Integer> bjIdsList = veBaseTeacherService.getBjIdsListBySysUserId(currentSemId, appUser.getUserId());
            if (bjIdsList.size()>0){
                QueryWrapper<VeBaseBanji> queryWrapper = new QueryWrapper();
                queryWrapper.in("id", bjIdsList);
                banjiList = veBaseBanjiService.list(queryWrapper);
            }

        }
        return Result.OK(banjiList);
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "排课节信息-通过id查询")
    @ApiOperation(value = "通过id查询", notes = "排课节信息-通过id查询")
    @PostMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        VeJwSection veJwSection = veJwSectionService.getById(id);
        if (veJwSection == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(veJwSection);
    }

//	/**
//	 * 新增信息
//	 * @param veJwSection
//	 * @return
//	 */
//	@AutoLog(value = "排课节信息-新增信息")
//	@ApiOperation(value="新增信息", notes="排课节信息-新增信息")
//	@PostMapping(value = "/add")
//	public Result<?> add(@RequestBody VeJwSection veJwSection) {
//		if(veJwSectionService.save(veJwSection)){
//		   return Result.OK("添加成功！");
//        }
//        return Result.error("添加失败!");
//	}

    /**
     * 修改信息
     *
     * @param veJwSection
     * @return
     */
    @AutoLog(value = "排课节信息-修改信息")
    @ApiOperation(value = "修改信息", notes = "排课节信息-修改信息")
    @PostMapping(value = "/edit")
    public Result<?> edit(@RequestBody VeJwSection veJwSection) {
        if (veJwSectionService.updateById(veJwSection)) {
            return Result.OK("编辑成功!");
        }
        return Result.error("编辑失败!");
    }

    @AutoLog(value = "排课节信息-设置可排课")
    @PostMapping("canSchedule")
    @ApiOperation(value = "设置可排课", notes = "排课节信息-设置可排课")
    public Result<?> canSchedule(@RequestParam(name = "ids", required = true) String ids) {
        if (this.veJwSectionService.changeStatusByIds(ids.split(","), 1) > 0) {
            return Result.OK("操作成功!");
        }
        return Result.error("操作失败");
    }

    @AutoLog(value = "排课节信息-设置不可排课")
    @PostMapping("canNotSchedule")
    @ApiOperation(value = "设置不可排课", notes = "排课节信息-设置不可排课")
    public Result<?> canNotSchedule(@RequestParam(name = "ids", required = true) String ids) {
        if (this.veJwSectionService.changeStatusByIds(ids.split(","), 0) > 0) {
            return Result.OK("操作成功!");
        }
        return Result.error("操作失败");
    }

//	/**
//	 * 通过id删除
//	 * @param id
//	 * @return
//	 */
//	@AutoLog(value = "排课节信息-通过id删除")
//	@ApiOperation(value="通过id删除", notes="排课节信息-通过id删除")
//	@PostMapping(value = "/delete")
//	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
//        if(veJwSectionService.removeById(id)){
//		    return Result.OK("删除成功!");
//        }
//        return Result.error("删除失败!");
//	}

//	/**
//	 * 批量删除
//	 * @param ids
//	 * @return
//	 */
//	@AutoLog(value = "排课节信息-批量删除")
//	@ApiOperation(value="批量删除", notes="排课节信息-批量删除")
//	@PostMapping(value = "/deleteBatch")
//	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
//		if(this.veJwSectionService.removeByIds(Arrays.asList(ids.split(",")))){
//		         return Result.OK("批量删除成功!");
//        }
//        return Result.error("批量删除失败!");
//	}

}
